#include "LocalScene.h"
#include "Utils/LogUtil.h"
#include "LocalEntity.h"
#include "Utils/StringUtil.h"
#include "Game/Component/Model.h"
#include "Render/RenderState.h"
#include "Render/Imgui/imgui.h"
#include "Math/Random.h"
#include "Game/GameState.h"
#include "Game/Component/MultiModel.h"
#include "ForwardPlusProcess.h"


void LocalScene::OnInit()
{
	mCamera.SetSpeed(6.f);

	mForwardPlusProcess = GetProcess<ForwardPlusProcess>("ForwardPlusProcess");

	mCoordinateProcess = GetProcess<CoordinateProcess>("CoordinateProcess");


	int32 i = 0;
	LocalEntity* sceneEntity = CreateEntity<LocalEntity>("scene");


	switch (i)
	{
	case 0:
	{
		sceneEntity->AddComponent<MultiModel>("VokseliaSpawn.obj");
		sceneEntity->GetTransform().SetLocalScale(glm::vec3(10.f));
		sceneEntity->GetTransform().UpdateMatrices();

		sceneEntity->mMaterial.hasAlbedoMap = 1;
		sceneEntity->mMaterial.hasNormalMap = 0;

		sceneEntity->mAlbedoPath = "VokseliaSpawn.png";
	}
	break;
	case 1:
	{
		sceneEntity->AddComponent<MultiModel>("LostEmpire.obj");
		// sceneEntity->GetTransform().SetLocalScale(glm::vec3(10.f));
		sceneEntity->GetTransform().UpdateMatrices();

		sceneEntity->mMaterial.hasAlbedoMap = 1;
		sceneEntity->mMaterial.hasNormalMap = 0;

		sceneEntity->mAlbedoPath = "LostEmpire.png";
	}
	break;
	case 2:
	{
		sceneEntity->AddComponent<MultiModel>("Sponza.obj");

		sceneEntity->mMaterial.hasAlbedoMap = 1;
		sceneEntity->mMaterial.hasNormalMap = 1;

		sceneEntity->mAlbedoPath = "Pbr\\Sponza_Albedo.jpg";
		sceneEntity->mNormalPath = "Pbr\\Sponza_Normal.png";
	}
		break;
	}





	mMinLightPos = glm::vec3(FLT_MAX);
	mMaxLightPos = glm::vec3(FLT_MIN);

	glm::mat4 modelMatrix = sceneEntity->GetTransform().GetWorldMatrix();
	MultiModel& model = sceneEntity->GetComponent<MultiModel>();
	int32 objectSize = model.mVertexGroups.size();

	for (int32 i = 0; i < objectSize; ++i)
	{
		for (int32 j = 0; j < model.mVertexGroups[i].size(); ++j)
		{
			glm::vec3 tempPos = glm::vec3(modelMatrix * glm::vec4(model.mVertexGroups[i][j].Position, 1.f));

			if (tempPos.x > mMaxLightPos.x)
				mMaxLightPos.x = tempPos.x;
			if (tempPos.y > mMaxLightPos.y)
				mMaxLightPos.y = tempPos.y;
			if (tempPos.z > mMaxLightPos.z)
				mMaxLightPos.z = tempPos.z;

			if (tempPos.x < mMinLightPos.x)
				mMinLightPos.x = tempPos.x;
			if (tempPos.y < mMinLightPos.y)
				mMinLightPos.y = tempPos.y;
			if (tempPos.z < mMinLightPos.z)
				mMinLightPos.z = tempPos.z;
		}
	}

	mLightRadius = 5.f;

	mLightNum = 1000;



}

void LocalScene::OnTick(float deltaTime)
{

}

void LocalScene::OnImgui()
{
	if (ImGui::BeginMainMenuBar())
	{
		ImGui::Text("Fps : %d ", GRenderState->GetFps());

		ImGui::RadioButton("Default", &mForwardPlusProcess->mPushConstants.debugIndex, 0);
		ImGui::RadioButton("HeatMap", &mForwardPlusProcess->mPushConstants.debugIndex, 1);
		ImGui::RadioButton("HeatMap Only", &mForwardPlusProcess->mPushConstants.debugIndex, 2);
		ImGui::RadioButton("PreDepth", &mForwardPlusProcess->mPushConstants.debugIndex, 3);
		ImGui::RadioButton("Normal", &mForwardPlusProcess->mPushConstants.debugIndex, 4);

		ImGui::EndMainMenuBar();
	}


}

void LocalScene::OnExit()
{

}